package com.ys.erp.stage_shop_service.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ys.erp.common.enums.EnableEnum;
import com.ys.erp.entity.data.bean.CsEmp;
import com.ys.erp.entity.data.bean.CsShiftSchedule;
import com.ys.erp.entity.data.bean.Employee;
import com.ys.erp.entity.service.ICsEmpService;
import com.ys.erp.entity.service.ICsShiftScheduleService;
import com.ys.erp.entity.service.IEmployeeService;
import com.ys.erp.framework.bean.dto.StageEmpDTO;
import com.ys.erp.stage_shop_service.bean.condition.CsShiftScheduleCondition;
import com.ys.erp.stage_shop_service.bean.vo.CsEmpVO;
import com.ys.erp.stage_shop_service.bean.vo.CsShiftScheduleVO;
import com.ys.erp.stage_shop_service.bean.vo.EmpVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author minamiyama
 * @date 2025/6/18 14:51
 */
@RequiredArgsConstructor
@Service
@Log4j
public class CustomerServiceService {

    private final ICsShiftScheduleService iCsShiftScheduleService;
    private final IEmployeeService iEmployeeService;
    private final ICsEmpService iCsEmpService;

    /**
     * 获取班次时间配置
     *
     * @param empDto 登录员工
     * @return 班次时间配置列表
     */
    public List<CsShiftScheduleVO> getShiftTimeConfig(StageEmpDTO empDto) {

        List<CsShiftSchedule> list = iCsShiftScheduleService.list();

        return list.stream().map(this::convertToVO).collect(Collectors.toList());
    }

    /**
     * 更新班次时间配置
     *
     * @param empDto 登录员工
     * @param con    请求条件
     */
    public void updateShiftTimeConfig(StageEmpDTO empDto, List<CsShiftScheduleCondition.UpdateShiftTimeCon> con) {
        List<CsShiftSchedule> csShiftSchedules = iCsShiftScheduleService.list();
        Map<String, CsShiftSchedule> csMap = csShiftSchedules.stream().collect(Collectors.toMap(CsShiftSchedule::getType, it -> it));
        ArrayList<CsShiftSchedule> schedules = new ArrayList<>();
        for (CsShiftScheduleCondition.UpdateShiftTimeCon updateShiftTimeCon : con) {
            CsShiftSchedule csShiftSchedule = csMap.get(updateShiftTimeCon.getType());
            if (Objects.isNull(csShiftSchedule)) {
                // 如果不存在，创建新配置
                csShiftSchedule = new CsShiftSchedule();
                csShiftSchedule.setType(updateShiftTimeCon.getType());
            }
            csShiftSchedule.setStartTime(updateShiftTimeCon.getStartTime());
            csShiftSchedule.setEndTime(updateShiftTimeCon.getEndTime());
            csShiftSchedule.setEmpId(empDto.getId());
            schedules.add(csShiftSchedule);
        }
        // 更新时间
        iCsShiftScheduleService.saveOrUpdateBatch(schedules);
    }


    /**
     * 转换为VO对象
     *
     * @param entity 实体对象
     * @return VO对象
     */
    private CsShiftScheduleVO convertToVO(CsShiftSchedule entity) {
        CsShiftScheduleVO vo = new CsShiftScheduleVO();
        BeanUtils.copyProperties(entity, vo);

        // 设置班次类型中文名称
        if (entity.getType() != null) {
            vo.setTypeName(CsShiftSchedule.ENUM_MAP.get(entity.getType()));
        }

        return vo;
    }

    public List<EmpVO> getEmpList() {
        LambdaQueryWrapper<Employee> empQw = new LambdaQueryWrapper<>();
        empQw.eq(Employee::getIsDisable, EnableEnum.NO.getEnable());
        empQw.eq(Employee::getShopCode, "hz0");
        List<Employee> employeeList = iEmployeeService.list(empQw);
        List<CsEmp> csEmpList = iCsEmpService.list();
        List<Long> csEmpIds = csEmpList.stream().map(CsEmp::getEmpId).toList();
        ArrayList<EmpVO> result = new ArrayList<>();
        for (Employee employee : employeeList) {
            if (csEmpIds.contains(employee.getId())) {
                continue;
            }
            EmpVO empVO = new EmpVO();
            BeanUtils.copyProperties(employee, empVO);
            result.add(empVO);
        }
        return result;

    }

    public void addCsEmp(StageEmpDTO empDto, List<CsShiftScheduleCondition.AddCsEmpCon> con) {
        if (CollectionUtils.isEmpty(con)) {
            return;
        }
        List<Long> empIdList = con.stream().map(CsShiftScheduleCondition.AddCsEmpCon::getEmpId).toList();
        List<Employee> employeeList = iEmployeeService.listByIds(empIdList);
        List<Employee> empList = employeeList.stream().filter(it -> "hz0".equals(it.getShopCode())).toList();
        Map<Long, Employee> empMap = empList.stream().collect(Collectors.toMap(Employee::getId, it -> it));
        List<Long> empIds = empList.stream().map(Employee::getId).toList();
        List<CsShiftScheduleCondition.AddCsEmpCon> conList = con.stream().filter(it -> empIds.contains(it.getEmpId())).toList();

        if (CollectionUtils.isEmpty(conList)) {
            return;
        }
        ArrayList<CsEmp> csEmpList = new ArrayList<>();
        for (CsShiftScheduleCondition.AddCsEmpCon addCsEmpCon : conList) {
            Employee employee = empMap.get(addCsEmpCon.getEmpId());
            //todo 后续添加企业微信判断
            CsEmp csEmp = new CsEmp();
            csEmp.setEmpName(addCsEmpCon.getEmpName());
            csEmp.setEmpId(addCsEmpCon.getEmpId());
            csEmp.setAvatar(employee.getPic());
            csEmpList.add(csEmp);
        }
        iCsEmpService.saveBatch(csEmpList);
    }

    public List<CsEmpVO> getCsEmpList() {
        List<CsEmp> csEmpList = iCsEmpService.list();
        ArrayList<CsEmpVO> result = new ArrayList<>();
        for (CsEmp csEmp : csEmpList) {
            CsEmpVO csEmpVO = new CsEmpVO();
            csEmpVO.setEmpName(csEmp.getEmpName());
            csEmpVO.setId(csEmp.getId());
            result.add(csEmpVO);
        }
        return result;
    }

    public void deleteCsEmp(StageEmpDTO empDto, Long id) {
        CsEmp csEmp = iCsEmpService.getById(id);
        if (Objects.isNull(csEmp)) {
            return;
        }
        iCsEmpService.removeById(id);
    }
}